package com.xgz.entity;

import java.lang.reflect.Field;

public class Test {
    public static void main(String[] args) {
        //获取class对象
/*        try {
            Class<?> aClass = Class.forName("com.xgz.entity.Animal");
            System.out.println("aClass = " + aClass);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }*/
        //2.通过类的class属性获取
        Class<Animal> animalClass = Animal.class;
        System.out.println("animalClass = " + animalClass);
        //3.通过对象的getClass方法
/*        Animal animal = new Animal();
        Class<? extends Animal> aClass = animal.getClass();
        System.out.println("aClass = " + aClass);*/

        try {
            //getField 获取某个共有的属性
            Field color = animalClass.getField( "color");
            System.out.println("getField()方法： " + color);
            //getDeclaredField可以获取共有的属性 私有的属性
            Field age = animalClass.getDeclaredField("age");
            System.out.println("getDeclaredField()方法： " + age);
            //获取所有的共有属性
            Field[] fields = animalClass.getFields();
            for (Field field : fields) {
                System.out.println("getFields()方法： " + field);
            }
            //获取所有的属性
            Field[] declaredFields = animalClass.getDeclaredFields();
            for (Field declaredField : declaredFields) {
                System.out.println("getDeclaredFields()方法： " + declaredField);
            }
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }

    }
}
